package com.ruoyi.project.business.dailyschedule.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.dailyschedule.entity.DailySchedule;
import com.ruoyi.project.business.dailyschedule.request.AIAssignDailyScheduleRequest;
import com.ruoyi.project.business.dailyschedule.request.AddDailyScheduleRequest;
import com.ruoyi.project.business.dailyschedule.request.ListDailyScheduleRequest;
import com.ruoyi.project.business.dailyschedule.response.DailyScheduleResponse;
import com.ruoyi.project.business.dailyschedule.service.IDailyScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 日排班Controller
 * 
 * @author ${author}
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/business/dailyschedule")
public class DailyScheduleController extends BaseController {
    @Autowired
    private IDailyScheduleService dailyScheduleService;

    /**
     * 查询日排班列表
     */
    @PreAuthorize("@ss.hasPermi('business:dailyschedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(ListDailyScheduleRequest request) {
        startPage();
        Long tenantId = getTenantId();
        List<DailyScheduleResponse> list = dailyScheduleService.selectDailyScheduleList(request, tenantId);
        return getDataTable(list);
    }

    /**
     * 导出日排班列表
     */
    @PreAuthorize("@ss.hasPermi('business:dailyschedule:export')")
    @Log(title = "日排班", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ListDailyScheduleRequest request) {
        Long tenantId = getTenantId();
        List<DailyScheduleResponse> list = dailyScheduleService.selectDailyScheduleList(request, tenantId);
        ExcelUtil<DailyScheduleResponse> util = new ExcelUtil<>(DailyScheduleResponse.class);
        return util.exportExcel(list, "dailyschedule");
    }

    /**
     * 获取日排班详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:dailyschedule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dailyScheduleService.selectDailyScheduleById(id));
    }

    /**
     * 新增日排班
     */
    @PreAuthorize("@ss.hasPermi('business:dailyschedule:add')")
    @Log(title = "日排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AddDailyScheduleRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(dailyScheduleService.insertDailySchedule(request, operatorId, tenantId));
    }

    /**
     * 修改日排班
     */
    @PreAuthorize("@ss.hasPermi('business:dailyschedule:edit')")
    @Log(title = "日排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DailySchedule dailySchedule) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(dailyScheduleService.updateDailySchedule(dailySchedule, operatorId, tenantId));
    }

    /**
     * 删除日排班
     */
    @PreAuthorize("@ss.hasPermi('business:dailyschedule:remove')")
    @Log(title = "日排班", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        return toAjax(dailyScheduleService.deleteDailyScheduleByIds(ids, operatorId, tenantId));
    }



    /**
     * 智能排班
     * 按当前设计是需要由人来指定需求 ，
     * 实际上是可能会做到通过预测来指定需求
     */
//    @PreAuthorize("@ss.hasPermi('business:dailyschedule:add')")
    @Log(title = "日排班", businessType = BusinessType.INSERT)
    @PostMapping("/ai-assign")
    public AjaxResult aiAssign(@RequestBody AIAssignDailyScheduleRequest request) {
        Long tenantId = getTenantId();
        Long operatorId = getUserId();
        int res = dailyScheduleService.aiAssign(request, operatorId, tenantId);
        return AjaxResult.success(res);
    }
}
